package net.minecraft.client.render.entity.model;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.model.Dilation;
import net.minecraft.client.model.ModelData;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.model.ModelPartBuilder;
import net.minecraft.client.model.ModelPartData;
import net.minecraft.client.model.ModelTransform;
import net.minecraft.client.model.TexturedModelData;
import net.minecraft.client.render.entity.state.HappyGhastEntityRenderState;

@Environment(EnvType.CLIENT)
public class HappyGhastEntityModel extends EntityModel<HappyGhastEntityRenderState> {
	public static final ModelTransformer BABY_TRANSFORMER = ModelTransformer.scaling(0.2375F);
	private static final float HARNESSED_SCALE = 0.9375F;
	private final ModelPart[] tentacles = new ModelPart[9];
	private final ModelPart body;

	public HappyGhastEntityModel(ModelPart modelPart) {
		super(modelPart);
		this.body = modelPart.getChild(EntityModelPartNames.BODY);

		for (int i = 0; i < this.tentacles.length; i++) {
			this.tentacles[i] = this.body.getChild(EntityModelPartNames.getTentacleName(i));
		}
	}

	public static TexturedModelData getTexturedModelData(boolean baby, Dilation dilation) {
		ModelData modelData = new ModelData();
		ModelPartData modelPartData = modelData.getRoot();
		ModelPartData modelPartData2 = modelPartData.addChild(
			EntityModelPartNames.BODY,
			ModelPartBuilder.create().uv(0, 0).cuboid(-8.0F, -8.0F, -8.0F, 16.0F, 16.0F, 16.0F, dilation),
			ModelTransform.origin(0.0F, 16.0F, 0.0F)
		);
		if (baby) {
			modelPartData2.addChild(
				EntityModelPartNames.INNER_BODY,
				ModelPartBuilder.create().uv(0, 32).cuboid(-8.0F, -16.0F, -8.0F, 16.0F, 16.0F, 16.0F, dilation.add(-0.5F)),
				ModelTransform.origin(0.0F, 8.0F, 0.0F)
			);
		}

		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(0),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 5.0F, 2.0F, dilation),
			ModelTransform.origin(-3.75F, 7.0F, -5.0F)
		);
		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(1),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 7.0F, 2.0F, dilation),
			ModelTransform.origin(1.25F, 7.0F, -5.0F)
		);
		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(2),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 4.0F, 2.0F, dilation),
			ModelTransform.origin(6.25F, 7.0F, -5.0F)
		);
		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(3),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 5.0F, 2.0F, dilation),
			ModelTransform.origin(-6.25F, 7.0F, 0.0F)
		);
		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(4),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 5.0F, 2.0F, dilation),
			ModelTransform.origin(-1.25F, 7.0F, 0.0F)
		);
		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(5),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 7.0F, 2.0F, dilation),
			ModelTransform.origin(3.75F, 7.0F, 0.0F)
		);
		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(6),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 8.0F, 2.0F, dilation),
			ModelTransform.origin(-3.75F, 7.0F, 5.0F)
		);
		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(7),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 8.0F, 2.0F, dilation),
			ModelTransform.origin(1.25F, 7.0F, 5.0F)
		);
		modelPartData2.addChild(
			EntityModelPartNames.getTentacleName(8),
			ModelPartBuilder.create().uv(0, 0).cuboid(-1.0F, 0.0F, -1.0F, 2.0F, 5.0F, 2.0F, dilation),
			ModelTransform.origin(6.25F, 7.0F, 5.0F)
		);
		return TexturedModelData.of(modelData, 64, 64).transform(ModelTransformer.scaling(4.0F));
	}

	public void setAngles(HappyGhastEntityRenderState happyGhastEntityRenderState) {
		super.setAngles(happyGhastEntityRenderState);
		if (!happyGhastEntityRenderState.harnessStack.isEmpty()) {
			this.body.xScale = 0.9375F;
			this.body.yScale = 0.9375F;
			this.body.zScale = 0.9375F;
		}

		GhastEntityModel.setTentacleAngles(happyGhastEntityRenderState, this.tentacles);
	}
}
